package com.lscl.rabbitmq.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class TestListener {

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues = "test_queue")
    public void test_queue_confirm(Message message) {

        String messageId = message.getMessageProperties().getMessageId();

        if (null == messageId) {

            System.out.println("消息id为null!");
            return;
        }

        if (redisTemplate.opsForValue().setIfAbsent(messageId, new String(message.getBody()))) {

            // 代表第一次消费消息
            System.out.println("消息消费成功: " + new String(message.getBody()));
        } else {
            System.out.println("消息已经被消费过了!");
        }
    }
}

